package com.lin.manager.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * @作者: 林江
 * @创建时间: 2024/3/28
 * @功能:
 */
public class DateTimeUtil {
    private static final String DATE_TIME = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE = "yyyy-MM-dd";
    private static final String YEAR_MONTH = "yyyy-MM";
    public static final String START_TIME = "startTime";
    public static final String END_TIME = "endTime";
    public static final String MIDDLE_TIME = "middle";

    public static Date convertStrToDate(String date) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_TIME);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String convertDateToStr(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_TIME);
        return format.format(date);
    }

    public static String convertDateToStrPattern(Date date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    public static Date convertStrToDatePattern(String date, String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取xxxx的第一天和最后一天的时间
     */
    public static Map<String, String> getFirstAndLast(Integer type) {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern(DATE);

        Map<String, String> map = new HashMap<>();
        if (type == 1) { // 本周 星期一 ~ 星期五
            map.put(START_TIME, getThisBeginWeek().format(pattern));
            map.put(END_TIME, getThisEndWeek().format(pattern));
        } else if (type == 2) { // 本月 1号 ~ 最后一天
            map.put(START_TIME, getThisBeginMonth().format(pattern));
            map.put(END_TIME, getThisEndMonth().format(pattern));
        } else if (type == 3) { // 本年 1月 ~ 12月
            map.put(START_TIME, getThisBeginYear().format(pattern));
            map.put(END_TIME, getThisEndYear().format(pattern));
        } else if (type == 4) { // 上周 星期一 ~ 星期五
            map.put(START_TIME, getLastBeginWeek().format(pattern));
            map.put(END_TIME, getLastEndWeek().format(pattern));
        } else if (type == 5) {// 上月 1号 ~ 最后一天
            map.put(START_TIME, getLastBeginMonth().format(pattern));
            map.put(END_TIME, getLastEndMonth().format(pattern));
        }

        return map;
    }

    /**
     * 本周一
     *
     * @return
     */
    public static LocalDateTime getThisBeginWeek() {
        LocalDateTime now = LocalDateTime.now();
        return now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
    }

    /**
     * 本周日
     *
     * @return
     */
    public static LocalDateTime getThisEndWeek() {
        LocalDateTime now = LocalDateTime.now();
        return now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
    }

    /**
     * 中间时间
     *
     * @return
     */
    public static List<String> getMiddleDateTime(String start, String end, Integer type) {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern(DATE);

        LocalDate startTime = LocalDate.parse(start);
        LocalDate endTime = LocalDate.parse(end);

        List<String> dates = new ArrayList<>();
        LocalDate temp = startTime;
        while (type != null && !temp.isAfter(endTime)) {
            if (type == 1 || type == 2 || type == 4 || type == 5) {
                dates.add(temp.format(pattern));
                temp = temp.plusDays(1);
            } else if (type == 3) {
                dates.add(temp.format(DateTimeFormatter.ofPattern(YEAR_MONTH)));
                temp = temp.plusMonths(1);
            }
        }

        return dates;
    }

    /**
     * 上周一
     *
     * @return
     */
    public static LocalDateTime getLastBeginWeek() {
        LocalDateTime now = LocalDateTime.now();
        return now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).minusWeeks(1);
    }

    /**
     * 上周日
     *
     * @return
     */
    public static LocalDateTime getLastEndWeek() {
        LocalDateTime now = LocalDateTime.now();
        return now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)).minusWeeks(1);
    }

    /**
     * 本月1号
     *
     * @return
     */
    public static LocalDateTime getThisBeginMonth() {
        LocalDateTime now = LocalDateTime.now();
        return now.with(TemporalAdjusters.firstDayOfMonth());
    }

    /**
     * 本月最后一天
     *
     * @return
     */
    public static LocalDateTime getThisEndMonth() {
        LocalDateTime now = LocalDateTime.now();
        return now.with(TemporalAdjusters.lastDayOfMonth());
    }

    /**
     * 本月1号
     *
     * @return
     */
    public static LocalDateTime getLastBeginMonth() {
        LocalDateTime now = LocalDateTime.now();
        return now.with(TemporalAdjusters.firstDayOfMonth()).minusMonths(1);
    }

    /**
     * 本月最后一天
     *
     * @return
     */
    public static LocalDateTime getLastEndMonth() {
        LocalDateTime now = LocalDateTime.now();
        return now.with(TemporalAdjusters.lastDayOfMonth()).minusMonths(1);
    }

    /**
     * 本年的1月1号
     *
     * @return
     */
    public static LocalDateTime getThisBeginYear() {
        LocalDateTime now = LocalDateTime.now();
        return now.with(TemporalAdjusters.firstDayOfYear());
    }

    /**
     * 本年的12月xx号
     *
     * @return
     */
    public static LocalDateTime getThisEndYear() {
        LocalDateTime now = LocalDateTime.now();
        return now.with(TemporalAdjusters.lastDayOfYear());
    }

    public static void main(String[] args) {
//        DateTimeUtil.getFirstAndLast(1);
        System.out.println(getFirstAndLast(2));

//        System.out.println("当前季度的开始"+getStartOrEndDayOfQuarter(LocalDate.now(),0,true));
//        System.out.println("当前季度的结束"+getStartOrEndDayOfQuarter(LocalDate.now(),0,false));
//        System.out.println("当前季度前一季度的开始"+getStartOrEndDayOfQuarter(LocalDate.now(),-1,true));
//        System.out.println("当前季度前一季度的结束"+getStartOrEndDayOfQuarter(LocalDate.now(),-1,false));
    }
}
